//
//  DrawCartoonPageContainer.m
//  drawacartoon
//
//  Created by Ji Hu on 12-8-13.
//  Copyright (c) 2012年 爱辅导. All rights reserved.
//

#import "DrawCartoonPageContainer.h"

@interface DrawCartoonPageContainer (Private)

//-(void)handleScrollGesture:(UIPanGestureRecognizer *)gestureRecognizer;
-(void)addPageToHead:(NSString *)animationID context:(void *)context;
-(void)addPageToTail:(NSString *)animationID context:(void *)context;

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
-(UIView*)getSubViewWithTag:(NSInteger)tag;

@end

@implementation DrawCartoonPageContainer

@synthesize delegate;

-(void)dealloc{
    self.delegate=nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithChildClass:(Class)childclass withFrame:(CGRect)frame{
    
    if ([childclass isSubclassOfClass:[UIView class]]==NO) {
        [Log error:@"not validate child class"];
        return nil;
    }
    
    self = [self initWithFrame:frame];
    
    if(self){
        _validateChildClass = childclass;
        
//        UIPanGestureRecognizer *gr=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScrollGesture:)];
//        [gr setMinimumNumberOfTouches:2];
//        [gr setMaximumNumberOfTouches:3];
//        [self addGestureRecognizer:gr];
//        [gr release];
        
      
        
        
    }
    
    return self;
}

-(NSInteger)getCurrentViewTag{
    return (headViewTag+tailViewTag)/2;
}

-(void)didMoveToSuperview{
    
    headViewTag=0;
    tailViewTag=Page_In_Content-1;
    
    for (int i =1 ; i < Page_In_Content ; i++) {
        
        CGRect childframe;
        
        if (i==[self getCurrentViewTag]) {
            childframe = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        }else {
            childframe = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
        }
        
        UIView *childView = [[_validateChildClass alloc] initWithFrame:childframe];
        childView.tag = i;
        [self addSubview:childView];
        
        [self.delegate didAddChild:childView withTag:i];
        
        [childView release];
    }
    
}

-(UIView *)getSubViewWithTag:(NSInteger)tag{
    for (UIView *view in self.subviews) {
        if (view.tag==tag) {
            return view;
        }
    }
    
    return nil;
}

-(void)addPageToTail:(NSString *)animationID context:(void *)context{
    
     UIView *headpage = [self getSubViewWithTag:headViewTag];
    [headpage retain];
    [headpage removeFromSuperview];
     headViewTag++;
    
    UIView *tailChild = [[_validateChildClass alloc] initWithFrame:CGRectMake(0, -self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
    tailChild.tag = tailViewTag+1;
    [self addSubview:tailChild];
    
    tailViewTag++;
    
    [self.delegate didAddPageToTail:headpage withAddedTailPage:tailChild];
    
    [headpage release];
    [tailChild release];
}

-(void)addPageToHead:(NSString *)animationID context:(void *)context{
    
    UIView *tailpage = [self getSubViewWithTag:tailViewTag];
    [tailpage retain];
    [tailpage removeFromSuperview];
    tailViewTag--;
    
    UIView *headchild = [[_validateChildClass alloc] initWithFrame:CGRectMake(-self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
    headchild.tag = headViewTag-1;
    [self addSubview:headchild];
        
    headViewTag--;
    
    [self.delegate didAddPageToHead:tailpage withAddedHeadPage:headchild];
    
    [tailpage release];
    [headchild release];

}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    
    if (finished) {
        
        if ([animationID isEqualToString:@"showNextPage"]) {
            [self addPageToTail:animationID context:context];
        }
        
    }
    
}

-(void)showLastPage{
    
   
    
}

-(void)showNextPage{
    
    UIView *currentpage = [self viewWithTag:[self getCurrentViewTag]];
    UIView *tailpage = [self getSubViewWithTag:tailViewTag];
    UIView *headpage = [self getSubViewWithTag:headViewTag];
    
    [UIView beginAnimations:@"showNextPage" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView setAnimationDuration:1.0];
    
    if ([self getCurrentViewTag]>1) {
        [UIView setAnimationDuration:0.5];
        headpage.center = CGPointMake(headpage.center.x, headpage.center.y+self.bounds.size.height);
    }
//    tailpage.alpha=0.1;
    currentpage.alpha=0.1;
    
    tailpage.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    
    [UIView commitAnimations];
    
    
}

//-(void)handleScrollGesture:(UIPanGestureRecognizer *)gestureRecognizer{
//    
//    //    CGPoint point=[gestureRecognizer locationInView:self];
//    if([gestureRecognizer numberOfTouches]<=1) return;
//    
//    CGPoint point = [gestureRecognizer locationOfTouch:0 inView:self];
//    
//    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
//        lastScrollTouch = point;
//    }
//    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
//        NSLog(@"gesture end.");  
//    }else if ( gestureRecognizer.state == UIGestureRecognizerStateCancelled){
//        NSLog(@"gesture cancel.");  
//    }
//    else if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized){
//        NSLog(@"gesture recognized.");  
//    }
//    else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
//        //         NSLog(@"gesture changed.");  
//        
//    }
//    
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
